﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.Maps.MapControl;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using Locator.Silverlight.Client.Models.InventoryServiceReference;

namespace Locator.Silverlight.Client.Models.ViewModels.Elements
{
    public class StoreWithInventoryViewModel : ViewModelBase
    {
        /// <summary>
        /// The <see cref="InventoryInfo" /> property's name.
        /// </summary>
        public const string InventoryInfoPropertyName = "InventoryInfo";

        private ObservableCollection<InventoryInfoViewModel> inventoryInfo;

        /// <summary>
        /// Gets the InventoryInfo property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<InventoryInfoViewModel> InventoryInfo
        {
            get { return inventoryInfo; }

            set
            {
                if (inventoryInfo == value)
                {
                    return;
                }

                inventoryInfo = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(InventoryInfoPropertyName);
            }
        }

        private StoreWithInventory storeWithInventory;

        public StoreWithInventoryViewModel(StoreWithInventory storeWithInventory)
        {
            this.storeWithInventory = storeWithInventory;
            if (this.storeWithInventory.Inventory == null)
            {
                throw new ArgumentNullException("storeWithInventory.Inventory", "The storeWithInventory's Inventory object is null");
            }
            else
            {
                this.InventoryInfo = new ObservableCollection<InventoryInfoViewModel>();
                foreach (InventoryInfo inventoryInfo in this.storeWithInventory.Inventory)
                {
                    this.InventoryInfo.Add(new InventoryInfoViewModel(inventoryInfo));
                }
            }
        }

        public string Address
        {
            get
            {
                return String.Format(CultureInfo.CurrentCulture,
                           "{0} {1}",
                           this.storeWithInventory.Store.Address,
                           this.storeWithInventory.Store.City);
            }
        }

        public Location Location
        {
            get
            {
                if (this.storeWithInventory.Store.Latitude.HasValue && this.storeWithInventory.Store.Longitude.HasValue)
                {
                    return new Location((double)this.storeWithInventory.Store.Latitude.Value, (double)this.storeWithInventory.Store.Longitude.Value);
                }
                else
                {
                    return null;
                }
            }
        }
    }
}